package com.baidu.process.chat.parser.sourceHandler.impl;

import com.alibaba.fastjson.JSONObject;
import com.baidu.process.chat.core.action.Actions;
import com.baidu.process.chat.core.web.bean.IMRequestServer;
import com.baidu.process.chat.parser.bean.QueryJsonStr;
import com.baidu.process.chat.parser.sourceHandler.BaseSourceHandler;
import com.baidu.process.config.service.OptAnswerService;
import com.baidu.process.ngd.bean.NGDResBody;
import com.baidu.process.record.bean.DialogueRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.baidu.process.constant.RedisKey.ClarfyKey;

/**
 * @Author:fangyuchao
 * @Description:
 * @Date: Create in 12:12 2022/11/11
 */
@Service
public class FaqHandler implements BaseSourceHandler {
    @Autowired
    private OptAnswerService optAnswerService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public QueryJsonStr handleQuery(NGDResBody ngdResBody, IMRequestServer imRequestServer, DialogueRecord dialogueRecord) {
        QueryJsonStr queryJsonStr = new QueryJsonStr();
        NGDResBody.NGDQueryData  queryData = ngdResBody.getData();
        Map<String,Object> debug = queryData.getDebug();
        Object detailObj = debug.get("detail");
        Map<String,Object> detail = JSONObject.parseObject(JSONObject.toJSONString(detailObj),Map.class);
        dialogueRecord.setIntentName(detail.get("standardQuestion").toString());
        String sessionId = queryData.getSessionId();
        //重置澄清次数
        redisTemplate.opsForValue().set(ClarfyKey+sessionId,"0",10, TimeUnit.MINUTES);
        Map<String,Object> answer = queryData.getAnswer();
        String answerText = answer.get("answerText").toString();
        queryJsonStr.setAnswer(answerText);
        queryJsonStr.setBot(Actions.DEFAULT.getCode());
        queryJsonStr.setSessionId(queryData.getSessionId());
        queryJsonStr.setContext(queryData.getContext());
        queryJsonStr.setSource(queryData.getSource());
        return queryJsonStr;
    }
}
